package com.example.note;

//import android.R.bool;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class NotatkaDB {

	private static final int DB_VERSION = 1;
	private static final String DB_NAME = "database.db";
	private static final String DB_TABLE = "dbnote";

	public static final String KEY_ID = "id";
	public static final String KEY_ID_OPTIONS = "INTEGER PRIMARY KEY AUTOINCREMENT";	//ID_OPTIONS
	public static final int ID_COLUMN = 0;
	public static final String KEY_TRESC = "tresc";	//KEY_tresc
	public static final String KEY_TRESC_OPTIONS = "TEXT NOT NULL";	//tresc_OPTIONS
	public static final int TRESC_COLUMN = 1;	//tresc_COLUMN
	public static final String KEY_PRIORYTET="priorytet";
	public static final String KEY_PRIORYTET_OPTIONS="INTEGER";
	public static final int PRIORYTET_COLUMN=2;
	public static final String KEY_ZAKONCZONY = "zakonczony";	//KEY_zakonczony
	public static final String KEY_ZAKONCZONY_OPTIONS = "INTEGER DEFAULT 0";	//zakonczony_OPTIONS
	public static final int ZAKONCZONY_COLUMN = 3;	//zakonczony_COLUMN

	private static final String DB_CREATE_NOTE_TABLE = 
			"CREATE TABLE " + DB_TABLE + "( " +
			KEY_ID + " " + KEY_ID_OPTIONS + ", " +
			KEY_TRESC + " " + KEY_TRESC_OPTIONS + ", " +
			KEY_PRIORYTET + " " + KEY_PRIORYTET_OPTIONS + ", " +
			KEY_ZAKONCZONY + " " + KEY_ZAKONCZONY_OPTIONS +
			");";
	private static final String DROP_NOTE_TABLE = 
			"DROP TABLE IF EXISTS " + DB_TABLE;

	private SQLiteDatabase db;
	private Context context;
	private DatabaseHelper dbHelper;

	private static class DatabaseHelper extends SQLiteOpenHelper {
		public DatabaseHelper(Context context, String name,
				CursorFactory factory, int version) {
			super(context, name, factory, version);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(DB_CREATE_NOTE_TABLE);

		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL(DROP_NOTE_TABLE);
			onCreate(db);
		}
	}

	public NotatkaDB(Context context) {
		this.context = context;
	}

	public NotatkaDB open(){
		dbHelper = new DatabaseHelper(context, DB_NAME, null, DB_VERSION);
		try {
			db = dbHelper.getWritableDatabase();
		} catch (SQLException e) {
			db = dbHelper.getReadableDatabase();
		}
		return this;
	}

	public void close() {
		dbHelper.close();
	}

	public long insertNotatka(String tresc,int priorytet) {	//insertNotatka
		ContentValues row = new ContentValues();	//zamiana na row z innej zmiennej
		row.put(KEY_TRESC, tresc);
		row.put(KEY_PRIORYTET,priorytet);	//+
		return db.insert(DB_TABLE, null, row);
	}

	public int updateNote(Notatka note) {	//updateTodo	(Todonote notek)
		long id = note.getId();
		String tresc = note.getTresc();
		int priorytet = note.getPriorytet();
		return updateNote(id, tresc, priorytet);
	}

	public int updateNote(long id, String tresc, int priorytet) {	//updateTodo	(long id, boolean comlpeted)
		//String queryUpdate="update dbnote set tresc='"+tresc+"', priorytet='"+priorytet+"' where id="+id+"";
		//db=dbHelper.getWritableDatabase();
		//db.execSQL(queryUpdate);
		String where = KEY_ID + "=" + id;
		//String where = "ID='" + id+"'";
		ContentValues updateNoteValues = new ContentValues();
		updateNoteValues.put(KEY_TRESC, tresc);
		updateNoteValues.put(KEY_PRIORYTET, priorytet);		//+
		return db.update(DB_TABLE, updateNoteValues, where, null);
	}

	public boolean deleteNote(long id){	//deleteTodo
		String where = KEY_ID + "=" + id;
		return db.delete(DB_TABLE, where, null) > 0;
	}

	public Cursor getAllNote() {	//getAllTodos
		String[] columns = {KEY_ID, KEY_TRESC, KEY_PRIORYTET, KEY_ZAKONCZONY};
		return db.query(DB_TABLE, columns, null, null, null, null, null);
	}

	public Notatka getNote(long id) {	//getTodo
		String[] columns = {KEY_ID, KEY_TRESC, KEY_PRIORYTET, KEY_ZAKONCZONY};
		String where = KEY_ID + "=" + id;
		Cursor cursor = db.query(DB_TABLE, columns, where, null, null, null, null);
		Notatka note = null;
		if(cursor != null && cursor.moveToFirst()) {
			String tresc = cursor.getString(TRESC_COLUMN);
			int priorytet = cursor.getInt(PRIORYTET_COLUMN);
			boolean zakonczony = cursor.getInt(ZAKONCZONY_COLUMN) > 0 ? true : false;
			note = new Notatka(id, tresc, priorytet, zakonczony);
		}
		return note;
	}
}